home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / vect / vectload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-29  |  3.5 KB  |  133 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12.  /*
  13.   * Geometry Routines
  14.   * 
  15.   * Geometry Supercomputer Project
  16.   * 
  17.   * ROUTINE DESCRIPTION:  Create and load a vert object from a file.
  18.   * 
  19.   */
  20.  
  21. #include "vectP.h"
  22.  
  23. /* why isn't VectCreate being called here ? */
  24.  
  25. Vect *
  26. VectFLoad(file, fname)
  27.     FILE *file;
  28.     char *fname;
  29. {
  30.     register Vect *v;
  31.     int     binary = 0, dimn = 3;
  32.     char *p;
  33.     int i;
  34.     static char badvert[] = "Reading VECT from \"%s\": bad %dth vertex (of %d)";
  35.  
  36.     if (file == NULL) return NULL;
  37.  
  38.     /* new format possibility: 4-d vectors */
  39.     if (fnextc(file, 1) == '4')    {
  40.     dimn = 4;    
  41.     if (fexpectstr(file,"4VECT")) return NULL;
  42.     }
  43.  
  44.     else if (fnextc(file, 0) != 'V' || fexpectstr(file, "VECT"))
  45.     return NULL;
  46.  
  47.     if(fnextc(file, 1) == 'B') {
  48.     if(fexpectstr(file, "BINARY"))
  49.         return NULL;
  50.     binary = 1;
  51.     if(fnextc(file, 1) == '\n')
  52.         (void) fgetc(file);        /* Toss \n */
  53.     }
  54.  
  55.     v = OOGLNewE(Vect, "VectFLoad: Vect");
  56.  
  57.     GGeomInit(v, VectMethods(), VECTMAGIC, NULL);
  58.     v->geomflags = (dimn == 4) ? VERT_4D : 0;
  59.     v->vnvert = NULL;
  60.     v->vncolor = NULL;
  61.     v->p = NULL;
  62.     v->c = NULL;
  63.  
  64.     if(fgetni(file, 1, &v->nvec, binary) <= 0 ||
  65.        fgetni(file, 1, &v->nvert, binary) <= 0 ||
  66.        fgetni(file, 1, &v->ncolor, binary) <= 0) {
  67.     OOGLSyntax(file, "Reading VECT from \"%s\": can't read header counts", fname);
  68.     goto bogus;
  69.     }
  70.     if(!vSane(v)) {
  71.     OOGLSyntax(file,
  72.      "Reading VECT from \"%s\": inconsistent VECT header counts %d %d %d",
  73.         fname, v->nvec, v->nvert, v->ncolor);
  74.     goto bogus;
  75.     }
  76.  
  77.     v->vnvert = OOGLNewNE(short, 2*v->nvec, "VECT nvec counts");
  78.     v->p = OOGLNewNE(HPoint3, v->nvert, "VECT vertices");
  79.     v->c = OOGLNewNE(ColorA, (v->ncolor>0) ? v->ncolor : 1, "VECT colors");
  80.  
  81.     v->vncolor = v->vnvert + v->nvec;
  82.  
  83.     if((i = fgetns(file, v->nvec, v->vnvert, binary)) < v->nvec) {
  84.     OOGLSyntax(file,
  85.      "Reading VECT from \"%s\": bad vertex count in %d'th polyline (of %d)",
  86.         fname, i, v->nvec);
  87.     goto bogus; 
  88.     }
  89.     if((i = fgetns(file, v->nvec, v->vncolor, binary)) < v->nvec) {
  90.     OOGLSyntax(file,
  91.      "Reading VECT from \"%s\": bad color count in %d'th polyline (of %d)",
  92.         fname, i, v->nvec);
  93.     goto bogus;
  94.     }
  95.     /* if the points are 3D points, we have to convert them to the native
  96.     4D data structure */
  97.     if (dimn == 3)    {
  98.     register HPoint3 *p;
  99.  
  100.     for(i = v->nvert, p = v->p; --i >= 0; p++) {
  101.         if (fgetnf(file, 3, (float *)p, binary) < 3) {
  102.         OOGLSyntax(file, badvert, fname, v->nvert - i, v->nvert);
  103.         goto bogus;
  104.         }
  105.         p->w = 1;
  106.       }
  107.     }
  108.     else {
  109.     i = fgetnf(file, 4*v->nvert, (float *)v->p, binary);
  110.     if(i < 4*v->nvert) {
  111.         OOGLSyntax(file, badvert, fname, i/4, v->nvert);
  112.         goto bogus;
  113.     }
  114.       }
  115.     if (v->ncolor > 0 &&
  116.     (i = fgetnf(file, 4*v->ncolor, (float *)v->c, binary)) < 4*v->ncolor) {
  117.         OOGLSyntax(file, "Reading VECT from \"%s\": bad %dth color (of %d)",
  118.         fname, i/4, v->ncolor);
  119.         goto bogus;
  120.     }
  121.  
  122.     if(!VectSane(v)) {
  123.     OOGLError(0, "Reading VECT from \"%s\": VECT polyline/color counts inconsistent with header", fname);
  124.     goto bogus;
  125.     }
  126.  
  127.     return v;
  128.  
  129.   bogus:
  130.     GeomDelete((Geom *)v);
  131.     return NULL;
  132. }
  133.